<?php
class HockiesController extends AppController {

	var $name = 'Hockies';

	function index($namhoc_id=null) {
		$this->Hocky->recursive = 0;
		$this->set('hockies', $this->paginate(array('namhoc_id'=>$namhoc_id)));
        $namhoc=$this->Hocky->query('select namhoc from namhocs where id='.$namhoc_id);
        $this->set(compact('namhoc_id','namhoc'));
	}

	function view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid hocky', true));
			$this->redirect(array('action' => 'index'));
		}
		$this->set('hocky', $this->Hocky->read(null, $id));
	}

	function add($namhoc_id=null) {
		if (!empty($this->data)) {
			$this->Hocky->create();
			$this->data['Hocky']['ngaybatdau'] = date('y-m-d',strtotime($this->data['Hocky']['ngaybatdauhk']));
			$this->data['Hocky']['ngayketthuc'] =  date('y-m-d',strtotime($this->data['Hocky']['ngayketthuchk']));
			
			if ($this->Hocky->save($this->data)) {
				$this->Session->setFlash(__('Thông tin đã lưu', true),'success');
				$this->redirect(array('action' => 'index',$namhoc_id));
			} else {
				$this->Session->setFlash(__('Thông tin không thể lưu', true),'error');
			}
		}
		$namhoc=$this->Hocky->query('select namhoc from namhocs where id='.$namhoc_id);
		$this->set(compact('namhoc','namhoc_id'));
	}

	function edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid hocky', true));
			$this->redirect(array('action' => 'index',$this->data['Hocky']['namhoc_id']));
		}
		if (!empty($this->data)) {
			$this->data['Hocky']['ngaybatdau'] = date('y-m-d',strtotime($this->data['Hocky']['ngaybatdauhk']));
			$this->data['Hocky']['ngayketthuc'] =  date('y-m-d',strtotime($this->data['Hocky']['ngayketthuchk']));
			if ($this->Hocky->save($this->data)) {
				$this->Session->setFlash(__('Thông tin đã lưu', true),'success');
				$this->redirect(array('action' => 'index',$this->data['Hocky']['namhoc_id']));
			} else {
				$this->Session->setFlash(__('Thông tin không thể lưu', true),'error');
			}
		}
		if (empty($this->data)) {
			$this->data = $this->Hocky->read(null, $id);
		}
		$namhocs = $this->Hocky->Namhoc->find('list',array('fields'=>'namhoc'));
		$this->set(compact('namhocs'));
	}

	function delete($id = null,$namhoc_id=null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for hocky', true));
			$this->redirect(array('action'=>'index',$namhoc_id));
		}
		if ($this->Hocky->delete($id)) {
			$this->Session->setFlash(__('Thông tin đã xóa', true),'success');
			$this->redirect(array('action'=>'index',$namhoc_id));
		}
		$this->Session->setFlash(__('Thông tin không thể xóa', true),'error');
		$this->redirect(array('action' => 'index',$namhoc_id));
	}
}
